<?php

class BetChk
{

    public $today;
    public $gametype;
    public $tbl;
    public $tblName;

    public function BetChk($today, $gametype = 1)
    {
        global $g_gametype_3d;
        $this->gametype = $gametype;
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->tblName = $g_gametype_3d[$gametype]['ch'];
        $this->today = $today;
    }

    public function chkBetDate()
    {
        include_once( "class_3d/MyLotto_3d.php" );
        global $conf;
        global $ui;
        global $show3D3S;
        $MyLotto_3d = new MyLotto_3d($this->gametype);
        $reVal = false;
        switch ($show3D3S)
        {
            case 0 :
                if ($lotto_data = $MyLotto_3d->getLotto3DByDate($this->today))
                {
                    $datehi = date("Hi");
                    if ($conf->get("stopbet_{$this->tbl}") <= $datehi)
                    {
                        $ui->showErrorPage("目前非{$this->tblName}下单时段！", true);
                        $reVal = false;
                        exit();
                    }
                    else
                    {
                        if ($datehi < $conf->get("startbet_{$this->tbl}"))
                        {
                            $ui->showErrorPage("目前非{$this->tblName}下单时段！", true);
                            $reVal = false;
                            exit();
                        }
                        else
                        {
                            if (0 < count(array_diff_assoc(array(0 - 1, 0 - 1, 0 - 1), array($lotto_data['n1'], $lotto_data['n2'], $lotto_data['n3']))))
                            {
                                $ui->showErrorPage("{$this->tblName}::第" . $lotto_data['round'] . "期<br />已截止下注！", true);
                                $reVal = false;
                                exit();
                            }
                            else
                            {
                                $reVal = $lotto_data;
                            }
                        }
                    }
                }
                else
                {
					$s=$conf->get("startbet_".$this->tbl);
					$startdate=date('Y-m-d H:i:s');
					$enddate=(date('H')<=10?date('Y-m-d'):date('Y-m-d',strtotime('1 day'))).' '.substr($s,0,2).':'.substr($s,2,2).':00';
					$r=timediff(strtotime($startdate),strtotime($enddate));
					$ui->showErrorPage("<div class='time2'><div class='timeDiv'>".sprintf('%02d',$r['hour']).":".sprintf('%02d',$r['min']).":".sprintf('%02d',$r['sec'])."</div></div>");
					//$ui->showErrorPage("目前无{$this->tblName}可下注期数！", true);
                    $reVal = false;
                    exit();
                }
                break;
            case 1 :
			case 2 :
			case 3 :
                if ($lotto_data = $MyLotto_3d->getLotto3SByDateTime())
                {
                    $reVal = $lotto_data;
                }
                else
                {
                    $lotto_data = $MyLotto_3d->getLottoNextDateTime();
                    if ($lotto_data)
                    {
                        $script = " <script>"
                                . "           //setTimeout('location.reload()', 9000);\n"
                                . "           var CountDownSecond = {$lotto_data['sec']};\n"
                                . "           CountDown();\n"
                                . "           function CountDown() {\n"
                                . "               if (CountDownSecond != 0) {\n"
                                . "                   CountDownSecond -= 1;\n"
                                . "                   document.getElementById('cut_time').innerHTML = '目前暂无{$this->tblName}可下注期数！！距离下一期开盘还有' + CountDownSecond + '秒';\n"
                                . "               }\n"
                                . "               else\n"
                                . "               {\n"
                                . "                   parent.mainFrame.location.reload();\n"
                                . "                   parent.leftFrame.location.reload();\n"
                                . "                   return;\n"
                                . "               }\n"
                                . "               setTimeout('CountDown()', 1000);\n"
                                . "           }\n"
                                . "   </script>\n"
                                . "";
                        unset($lotto_data);
                        $ui->showErrorPage($script, true);
                    }
                    else
                    {
						$s=$conf->get("startbet_".$this->tbl);
						$startdate=date('Y-m-d H:i:s');
						$enddate=(date('H')<=10?date('Y-m-d'):date('Y-m-d',strtotime('1 day'))).' '.substr($s,0,2).':'.substr($s,2,2).':00';
						$r=timediff(strtotime($startdate),strtotime($enddate));
						$ui->showErrorPage("<div class='time2'><div class='timeDiv'>".sprintf('%02d',$r['hour']).":".sprintf('%02d',$r['min']).":".sprintf('%02d',$r['sec'])."</div></div>");
                        //$ui->showErrorPage("目前暂无{$this->tblName}可下注期数！", true);
                    }
                    $reVal = false;
                    exit();
                }
                break;
            /*case 2 :
                if ($lotto_data = $MyLotto_3d->getLotto3SByDateTime())
                {
                    $reVal = $lotto_data;
                }
                else
                {
                    $ui->showErrorPage("目前暂无{$this->tblName}可下注期数！", true);
                    $reVal = false;
                    exit();
                }
                break;*/
            default :
                exit("error 3d3s");
        }
        return $reVal;
    }

}
function timediff($begin_time,$end_time) 
{ 
      if($begin_time < $end_time){ 
         $starttime = $begin_time; 
         $endtime = $end_time; 
      } 
      else{ 
         $starttime = $end_time; 
         $endtime = $begin_time; 
      } 
      $timediff = $endtime-$starttime; 
      $days = intval($timediff/86400); 
      $remain = $timediff%86400; 
      $hours = intval($remain/3600); 
      $remain = $remain%3600; 
      $mins = intval($remain/60); 
      $secs = $remain%60; 
      $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs); 
      return $res; 
}
?>